import os
from time import sleep

import yaml
from selenium import webdriver
from faker import Faker
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from qiyeweixin_testing.until.until import mock_user_date

date_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'data', 'user_date.yml')


class BasePage:
    _BASE_URL = ""
    _faker = Faker(locale="zh_CN")

    def __init__(self, driver=None):
        if driver:
            self.driver = driver
        else:
            self.driver = webdriver.Chrome()
            self.driver.implicitly_wait(5)
            # self.driver.maximize_window()

        if not self.driver.current_url.startswith('http'):
            self.driver.get(self._BASE_URL)

    def do_quit(self):
        self.driver.quit()

    def do_find(self, by, locator=None):
        if locator:
            return self.driver.find_element(by, locator)
        else:
            return self.driver.find_element(*by)

    def do_finds(self, by, locator=None):
        if locator:
            return self.driver.find_elements(by, locator)
        else:
            return self.driver.find_elements(*by)

    def do_send_keys(self, msg, by, locator=None):
        ele = self.do_find(by, locator)
        ele.click()

        return ele.send_keys(msg)

    def wait_until_click(self, by):
        return WebDriverWait(self.driver, 10).until(expected_conditions.element_to_be_clickable(by))

    def wait_until_not_element(self, by):
        return WebDriverWait(self.driver, 10).until_not(expected_conditions.visibility_of_element_located(by))

    def wait_until_element(self, by):
        return WebDriverWait(self.driver, 10).until(expected_conditions.visibility_of_element_located(by))
